package oop.core.parsers;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import oop.core.entity.Stats;
import oop.core.item.Equipment;
import oop.core.item.EquippableItem;
import oop.core.item.EquippableItemPosition;

/** 
 *converts an avatar's equipment into a string format
 *we were planning on useing to implement saving and loading.
 * @author srhinson
 *
 */
public class EquipmentParser {
	static String del = "E~";
	static String specdel = "~~~";
	static String stringdel = "S~";
	
	public static String parseOut(Equipment equipment){
		String data = "";
		for (int i = 0; i<5;i++){
			if(equipment.getItem(i) != null){
				for ( String s : equipment.getItem(i).getOccupations()){
			
					data=data.concat(s+stringdel);
				
				}
				
				data=data.concat(del);
				data=data.concat(equipment.getItem(i).getItemPosition()+del);
				data=data.concat(equipment.getItem(i).getImageID()+del);
				data=data.concat(equipment.getItem(i).getEquipmentType()+del);
				data=data.concat(equipment.getItem(i).getName()+del);
				data=data.concat(equipment.getItem(i).getPrice()+del);
				data=data.concat(StatsParser.parseOut(equipment.getItem(i).getStats())+del);
				data=data.concat(specdel);
			}
		}
		System.out.println("HEre is the DATA:" + data);
		return data;
	}
	
	private static List<String> stringHelper(String data){
		Scanner scanner = new Scanner(data);
		scanner.useDelimiter(stringdel);
		List<String> strings = new ArrayList<String>();
		while(scanner.hasNext()){
			strings.add(scanner.next());
		}
		return strings;
	}
	
	public static Equipment parseIn(String data){
		Scanner scanner = new Scanner(data);
		scanner.useDelimiter(del);
		EquippableItem e[] = new EquippableItem[5];
		for(int i=0;i<5;i++){
			List<String> strings = stringHelper(scanner.next());
			String check = scanner.next();
			EquippableItemPosition position;
				position=EquippableItemPosition.Head;
			if(check=="Torso"){
				position=EquippableItemPosition.Torso;
			}
			if(check=="LeftArm"){
				position=EquippableItemPosition.LeftArm;
			}
			if(check=="RightArm"){
				position=EquippableItemPosition.RightArm;
			}
			if(check=="Legs"){
				position=EquippableItemPosition.Legs;
			}
			String image = scanner.next();
			String type = scanner.next();
			String name = scanner.next();
			int price = Integer.parseInt(scanner.next());
			Stats stats = StatsParser.parseIn(scanner.next());
			
			e[i]= new EquippableItem(name, image, price, stats, strings, position, type);
		}
		return new Equipment(e);
	}

}
